package main

import (
	"encoding/json"
	"mscoin-common/kafkaTest/kafkaTest"
)

type Data struct {
	Id   string `json:"id"`
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func main() {

	client := kafkaTest.NewKafkaClient(kafkaTest.KafkaConfig{
		Addr:          "192.168.200.161:9092",
		WriteCap:      100,
		ReadCap:       100,
		ConsumerGroup: "exchange-consumer",
	})
	//直接发送消息
	//DirectionSendData(client)
	SendData(client)

	//优雅的额退出
	//go func() {
	//	exit := make(chan os.Signal)
	//	signal.Notify(exit, syscall.SIGINT, syscall.SIGTERM)
	//	select {
	//	case <-exit:
	//		fmt.Println("生产者close")
	//		client.Close()
	//	}
	//}()
	for {

	}
}

// 直接发送数据
func DirectionSendData(client *kafkaTest.KafkaClient) {
	//直接发送消息
	msg := &Data{
		Id:   "1",
		Name: "mike",
		Age:  12,
	}
	m, _ := json.Marshal(msg)
	//直接发送消息
	directionData := kafkaTest.KafkaData{
		Topic: "test",
		Key:   []byte("1"),
		Data:  m,
	}
	client.SendSync(directionData)
}

// 通过通道间接发送数据
func SendData(client *kafkaTest.KafkaClient) {
	client.StartWrite()
	//直接发送消息
	msg := &Data{
		Id:   "2",
		Name: "lili",
		Age:  16,
	}
	m, _ := json.Marshal(msg)
	//直接发送消息
	show := kafkaTest.KafkaData{
		Topic: "test",
		Key:   []byte("2"),
		Data:  m,
	}
	client.Send(show)
}
